﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Common;
using H2.NET.Web.Workflow.DwzCode;
using Ultimus.OC;
using Ultimus.WFServer;
using UServLib;

namespace H2.NET.Web.Workflow.Workflow
{
    public partial class AssignConfer : BasePage
    {
        public string[] CurrentTaskIds;

        protected void Page_Load(object sender, EventArgs e)
        {
            var taskIds = AksRequest.GetString("ids");
            CurrentTaskIds = string.IsNullOrEmpty(taskIds) ? new string[0] : taskIds.Split(',');

            var action = AksRequest.GetQueryStringValue("action");
            if (string.IsNullOrEmpty(action)) //默认页面加载
            {
                
            }
            else if (action == "save") //form提交
            {
                var flag = false;

                var strAssignConferToUser = AksRequest.GetString("assignConferTo");
                #region 从OC中查找用户信息
                User userAssignConferTo = null;
                var ocAssignConferToUser = new OrgChart();
                if (!ocAssignConferToUser.FindUser(strAssignConferToUser, "", 0, out userAssignConferTo))
                {
                    ErrorMsg("找不到此用户" + strAssignConferToUser);
                    return;
                }
                #endregion

                var actionType = AksRequest.GetFormIntValue("actionType");
                var option = AksRequest.GetFormIntValue("option");
                switch (option)
                {
                    case 1:
                        {
                            #region 仅限选择的任务
                            
                            foreach (var taskId in CurrentTaskIds)
                            {
                                if (actionType == 1) //指派
                                {
                                    var task = new Task();
                                    task.InitializeFromTaskId(taskId);
                                    flag = task.AssignTask(userAssignConferTo.strUserName);

                                    //追加任务指派记录
                                    AssignConferTo(task.strProcessName, task.nIncidentNo.ToString(), task.strStepName, clsCmm.comm_username, clsCmm.comm_userfullname
                                        , userAssignConferTo.strUserName, userAssignConferTo.strUserFullName, "AssignTo");
                                }
                                else if (actionType == 2) //会办
                                {
                                    flag = ConferToTask(clsCmm.comm_username, taskId, userAssignConferTo.strUserName);
                                }
                            }
                            #endregion
                        }
                        break;
                    case 2:
                        {
                            #region 所有当前的任务

                            User usrTemp = null;
                            var ocTemp = new OrgChart();
                            ocTemp.FindUser(clsCmm.comm_username, "", 0, out usrTemp);

                            //加载任务
                            var filters = new TasklistFilter
                            {
                                nFiltersMask = Filters.nFilter_Current,
                                strArrUserName = new string[1] { clsCmm.comm_username }
                            };

                            var list = new Tasklist();
                            list.LoadFilteredTasks(filters);

                            if (actionType == 1) //指派
                            {
                                if (list.GetTasksCount() > 0)
                                {
                                    var task = list.GetNextTask();

                                    //追加任务指派记录
                                    AssignConferTo(task.strProcessName, task.nIncidentNo.ToString(), task.strStepName,
                                        clsCmm.comm_username, clsCmm.comm_userfullname
                                        , userAssignConferTo.strUserName, userAssignConferTo.strUserFullName, "AssignTo");
                                }

                                flag = usrTemp.AssignAllCurrentTasks(userAssignConferTo.strUserName);
                            }
                            else if (actionType == 2) //会办
                            {
                                if (list.GetTasksCount() > 0)
                                {
                                    var task = list.GetNextTask();
                                    flag = ConferToTask(clsCmm.comm_username, task.strTaskId, userAssignConferTo.strUserName);
                                }
                            }
                            #endregion
                        }
                        break;
                    case 3:
                        {
                            #region 所有将来的任务
                            if (actionType == 1) //指派
                            {
                                User usrTemp = null;
                                var ocTemp = new OrgChart();
                                ocTemp.FindUser(clsCmm.comm_username, "", 0, out usrTemp);

                                //日期数据检查
                                var dteUntileDate = System.DateTime.Now.AddYears(20);
                                var strUntileDate = AksRequest.GetFormString("untileDate");
                                if (!string.IsNullOrEmpty(strUntileDate))
                                {
                                    dteUntileDate = System.Convert.ToDateTime(strUntileDate);
                                }

                                flag = usrTemp.AssignAllFutureTasks(userAssignConferTo.strUserName, dteUntileDate.ToOADate());
                            }

                            #endregion
                        }
                        break;
                }

                if (flag)
                {
                    SuccessMsg("ToDoList");
                }
                else
                {
                    ErrorMsg("操作失败");
                }
            }
        }

        private void AssignConferTo(string strProcess, string strIncident, string strStep, string strUserName, string strUserFullName, string strAssignedToUserName, string strAssignedToUserFullName, string strAssignedClass)
        {
            try
            {
                clsCmm.clsDB.Storage("oleDB");
                var dt = clsCmm.clsDB.Query("select * from t_AssignedTo where Process='" + strProcess + "' and Incident=" + strIncident + " and Step='" + strStep + "' and Time is null");
                if (dt.Rows.Count >= 1)
                {
                    var i = clsCmm.clsDB.ExcuteSql("update t_AssignedTo set Time=getdate() where Process='" + strProcess + "' and Incident=" + strIncident + " and Step='" + strStep + "' and Time is null");
                }

                var strInsert = "insert into t_AssignedTo(ID,Process,Incident,Step,UserName,UserFullName,AssignedToUserName,AssignedToUserFullName,AssignedClass,StartTime) values('"
                      + System.DateTime.Now.ToString("yyyyMMddHHmmss") + dt.GetHashCode().ToString() + "'"
                      + ",'" + strProcess + "'"
                      + "," + strIncident
                      + ",'" + strStep + "'"
                      + ",'" + strUserName + "'"
                      + ",'" + strUserFullName + "'"
                      + ",'" + strAssignedToUserName + "'"
                      + ",'" + strAssignedToUserFullName + "'"
                      + ",'" + strAssignedClass + "'"
                      + ",getdate()"
                      + ")";

                clsCmm.clsDB.ExcuteSql(strInsert);
            }
            catch (Exception ex)
            {

            }
        }

        private bool ConferToTask(string strUser, string strTaskId, string strAssignedTo)
        {
            var o = new UTLServClass();
            bool flag;
            try
            {
                o.ConferTasks(0, 0, 0, strUser, strTaskId, strAssignedTo);
                flag = true;

                //追加任务指派记录
                var tskTemp = new Task();
                tskTemp.InitializeFromTaskId(strTaskId);
                AssignConferTo(tskTemp.strProcessName, tskTemp.nIncidentNo.ToString(), tskTemp.strStepName, clsCmm.comm_username, clsCmm.comm_userfullname
                             , strAssignedTo, strAssignedTo, "ConferTo");
            }
            catch
            {
                flag = false;
            }
            finally
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                catch
                {

                }
            }
            return flag;
        }

    }
}